package com.achao.service.impl;

import com.achao.dao.UserDao;
import com.achao.service.UserService;
import com.achao.utils.MD5Utils;
import com.achao.utils.MybatisUtils;
import com.achao.utils.ParamUtils;
import com.achao.utils.StringUtils;
import com.achao.view.UserIndexView;
import com.achao.view.UserLoginView;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;

/**
 * @Author: achao
 * @DateTime: 2022/7/24 15:22
 * @Description: TODO
 */
public class UserServiceImpl implements UserService {

    @Override
    public void frontLogin() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        UserLoginView instance = UserLoginView.getInstance();
        String userName = instance.getUserNameText().getText();
        String userPwd = instance.getUserPwdText().getText();
        if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(userPwd)) {
            // 检测账号是否存在
            int id = userDao.getIdByUserName(userName);
            if (id > 0) {
                String pwd = userDao.getPwdById(id);
                if (pwd.equals(MD5Utils.md5(userPwd + ParamUtils.SECRET))) {
                    userDao.updateLoginStatus(id);
                    instance.setVisible(false);
                    UserIndexView.getInstance().setVisible(true);
                    JOptionPane.showMessageDialog(instance, "登录成功,跳转首页");
                } else {
                    JOptionPane.showMessageDialog(instance, "登录失败,密码错误");
                }
            } else {
                JOptionPane.showMessageDialog(instance, "用户不存在!");
            }
        } else {
            JOptionPane.showMessageDialog(instance, "请完善信息!");
        }

    }
}
